/**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file FormAlarmAwakeType.cpp
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#include "forms/FormAlarmAwakeType.h"
#include "SCFormManager.h"
#include "SCTranslator.h"

FormAlarmAwakeType::FormAlarmAwakeType()
{
}

FormAlarmAwakeType::~FormAlarmAwakeType()
{
}

bool
FormAlarmAwakeType::Initialize()
{
	Construct(L"IDF_ALARM_AWAKE_TYPE");
	return true;
}

result
FormAlarmAwakeType::OnInitializing(void)
{
	result r = E_SUCCESS;

	ScrollPanel* pScrollPanel = static_cast<ScrollPanel*>(GetControl("IDC_SCROLLPANEL_AWAKE_TYPE"));

	this->pSound = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_SOUND"));
	this->pVibration = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_VIBRATION"));
	this->pSoundVibration = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_SOUND_VIBRATION"));
	this->pSound->SetActionId(AWAKE_TYPE_SOUND, AWAKE_TYPE_SOUND);
	this->pVibration->SetActionId(AWAKE_TYPE_VIBRATION, AWAKE_TYPE_VIBRATION);
	this->pSoundVibration->SetActionId(AWAKE_TYPE_SOUND_VIBRATION, AWAKE_TYPE_SOUND_VIBRATION);
	this->pSound->AddActionEventListener(*this);
	this->pVibration->AddActionEventListener(*this);
	this->pSoundVibration->AddActionEventListener(*this);

	//language
	this->SetTitleText(SCTranslator::GetText("IDS_ALARMSTYPE_TYPE"));
	this->pSound->SetText(SCTranslator::GetText("IDS_ALARMSTYPE_SOUND"));
	this->pVibration->SetText(SCTranslator::GetText("IDS_ALARMSTYPE_VIBRATION"));
	this->pSoundVibration->SetText(SCTranslator::GetText("IDS_ALARMSTYPE_SOUNDVIBRATION"));

	Footer* pFooter = this->GetFooter();
	FooterItem* pBack = new FooterItem();
	pBack->Construct(IDA_BUTTON_BACK);
	pBack->SetText(SCTranslator::GetText("IDS_ALARMS_BACK"));
	//this->pFooter->SetBackButton();

	pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
	pFooter->AddItem(*pBack);

	pFooter->AddActionEventListener(*this);

	delete pBack;

	return r;
}

result
FormAlarmAwakeType::OnTerminating(void)
{
	result r = E_SUCCESS;
	return r;
}

void
FormAlarmAwakeType::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	ArrayList* pParams;

	switch(actionId)
	{
		case IDA_BUTTON_BACK:
			pParams = new ArrayList();
			pParams->Add(*this->pAlarm);
			SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM_SET, false, pParams, SCFormManager::SLIDE_RIGHT);
			pParams->RemoveAll();
			delete pParams;
			break;
		case AWAKE_TYPE_SOUND:
			if(this->pAlarm != NULL)
			{
				this->pAlarm->SetAwakeType(actionId);
			}
			pParams = new ArrayList();
			pParams->Add(*this->pAlarm);
			SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM_SET, false, pParams, SCFormManager::SLIDE_RIGHT);
			pParams->RemoveAll();
			delete pParams;
			break;
		case AWAKE_TYPE_VIBRATION:
			if(this->pAlarm != NULL)
			{
				this->pAlarm->SetAwakeType(actionId);
			}
			pParams = new ArrayList();
			pParams->Add(*this->pAlarm);
			SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM_SET, false, pParams, SCFormManager::SLIDE_RIGHT);
			pParams->RemoveAll();
			delete pParams;
			break;
		case AWAKE_TYPE_SOUND_VIBRATION:
			if(this->pAlarm != NULL)
			{
				this->pAlarm->SetAwakeType(actionId);
			}
			pParams = new ArrayList();
			pParams->Add(*this->pAlarm);
			SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM_SET, false, pParams, SCFormManager::SLIDE_RIGHT);
			pParams->RemoveAll();
			delete pParams;
			break;
		default:
			AppLog("Unknown click - %d.", actionId);
			break;
	}
}

void
FormAlarmAwakeType::Update()
{
	if(this->pAlarm != NULL)
	{
		int type = this->pAlarm->GetAwakeType();

		if(type == AWAKE_TYPE_SOUND)
		{
			this->pSound->SetSelected(true);
			this->pVibration->SetSelected(false);
			this->pSoundVibration->SetSelected(false);
		} else if(type == AWAKE_TYPE_VIBRATION)
		{
			this->pSound->SetSelected(false);
			this->pVibration->SetSelected(true);
			this->pSoundVibration->SetSelected(false);
		} else if(type == AWAKE_TYPE_VIBRATION)
		{
			this->pSound->SetSelected(false);
			this->pVibration->SetSelected(false);
			this->pSoundVibration->SetSelected(true);
		}
	}
}

void
FormAlarmAwakeType::Redraw()
{
	this->Update();
	RequestRedraw(true);
}

int
FormAlarmAwakeType::GetFormId()
{
	return SCFormManager::IDF_ALARM_AWAKE_TYPE;
}

void
FormAlarmAwakeType::SetAlarm(SCAlarm* pAlarm)
{
	this->pAlarm = pAlarm;
}

SCAlarm*
FormAlarmAwakeType::GetAlarm()
{
	return this->pAlarm;
}
